package com.eshop.dao.provider;

import com.eshop.pojo.OrderInfo;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class OrderInfoDynSqlProvider {
    public String selectByPage(Map<String ,Object> param){ //分页查询订单信息
        String sql = new SQL(){
            {
                SELECT("*");
                FROM("order_info");
                if (param.get("orderInfo") != null){
                    OrderInfo orderInfo = (OrderInfo) param.get("orderInfo");
                    if (orderInfo.getId() > 0){
                        WHERE("id=#{orderInfo.id}");
                    }else {
                        if (orderInfo.getUid() > 0){
                            WHERE("uid=#{orderInfo.uid}");
                        }
                        if (orderInfo.getStatus() != null && !orderInfo.getStatus().equals("请选择...")){
                            WHERE("status=#{orderInfo.status}");
                        }
                        if (orderInfo.getOrderTimeFrom() != null){
                            WHERE("ordertime >= #{orderInfo.orderTimeFrom}");
                        }
                        if (orderInfo.getOrderTimeTo() != null){
                            WHERE("ordertime <= #{orderInfo.orderTimeTo}");
                        }
                    }
                }
            }
        }.toString();
        if (param.get("pager")  != null) {
            sql += " limit #{pager.firstLimitParam} , #{pager.perPageRows}";
        }
        //System.out.println(sql);//调试
        return sql;
    }

    public String count(Map<String ,Object> param){ //根据参数查询订单总数
        return new SQL(){
            {
                SELECT("count(*)");
                FROM("order_info");
                if (param.get("orderInfo") != null){
                    OrderInfo orderInfo = (OrderInfo) param.get("orderInfo");
                    if (orderInfo.getId() > 0){
                        WHERE("id=#{orderInfo.id}");
                    }else {
                        if (orderInfo.getUid() > 0){
                            WHERE("uid=#{orderInfo.uid}");
                        }
                        if (orderInfo.getStatus() != null && !orderInfo.getStatus().equals("请选择...")){
                            WHERE("status=#{orderInfo.status}");
                        }
                        if (orderInfo.getOrderTimeFrom() != null){
                            WHERE("ordertime >= #{orderInfo.orderTimeFrom}");
                        }
                        if (orderInfo.getOrderTimeTo() != null){
                            WHERE("ordertime <= #{orderInfo.orderTimeTo}");
                        }
                    }
                }
            }
        }.toString();
    }
}
